package com.lsh.airticket.controller.admin;

import com.lsh.airticket.dto.Result;
import com.lsh.airticket.entity.Space;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.SpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lsh
 * @date 2021/2/18
 **/
@RestController
@RequestMapping("/flights")
public class AdminSpaceController {

    @Autowired
    SpaceService spaceService;

    /**
     * 查询所有的 飞机舱位
     */
    @RequestMapping("/admin/space/spaceList")
    public Result findSpaceList() {
        List<Space> spaceList = spaceService.findSpaceList();
        return Result.result().addData("spaceList", spaceList);
    }


    /**
     * 创建一个space
     * @param space
     * @return
     */
    @PostMapping("/admin/space/insertSpace")
    public Result insertSpace(@RequestBody Space space) {
        int i = spaceService.insertSpace(space);
        if (i != 1) {
            throw new UserException("添加失败");
        }
        return Result.result().setMsg("添加成功");
    }

    /**
     * 通过modelId查找 飞机的舱位
     * @param modelId
     * @return
     */
    @GetMapping("/admin/space/findSpaceByModelId")
    public Result findSpaceByModelId(Integer modelId) {
        List<Space> spaceList = spaceService.findSpaceByModelId(modelId);
        return Result.result().addData("spaceList",spaceList);
    }

    /**
     * 修改一条数据
     * @return
     */
    @PostMapping("/admin/model/updateSpace")
    public Result updateSpace(@RequestBody Space space) {
        int update = spaceService.updateSpace(space);
        if (update != 1) {
            throw new UserException("修改失败");
        }
        return Result.result().setMsg("修改成功");
    }

    /**
     * 根据主键删除一条数据
     */
    @GetMapping("/admin/model/deleteSpace")
    public Result deleteModel(Integer spaceId) {
        int delete = spaceService.deleteSpaceById(spaceId);
        if (delete != 1) {
            throw new UserException("删除失败");
        }
        return Result.result().setMsg("删除成功");
    }


}
